<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\Box;
use App\Models\BoxAddress;
use App\Models\Colour;
use App\Models\BoxDeliver;
use App\Models\Comment;
use App\Models\BoxCollocation;
use App\Models\BoxCare;
use App\Models\UsersWechat;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class BoxCareController extends Controller
{
    public function __construct()
    {
        $this->middleware('admin');
    }


    public function getBoxCare(Request $request){
        $data['colours'] = Colour::get();
        return view('admin/box/box_care_list',$data);
    }

    public function getBoxCarePageRows(Request $request){
        $status = intval($request->input('status'));
        $page = intval($request->input('page',1));
        $number = safe2($request->input('number'));//会员编号
        $express_number = safe2($request->input('express_number'));//会员编号
        $delivery_at = $request->input('delivery_at');//发货时间

        $UsersWechat = UsersWechat::select('id')->where('number',$number)->first();

        if ($number) {
            if ($UsersWechat) {
                $query = BoxCare::whereIn('wechat_id',$UsersWechat->id)->orderBy('id','DESC');
            }else{
                unset($UsersWechat);
                return $this->Error('暂无查询记录');
            }
        }else{
            $query = BoxCare::orderBy('id','DESC');
        }

        if ($express_number) {
            $query->where('send','like',"%{$express_number}%");
        }

        if ($delivery_at) {
            $query->whereDate('delivery_at',$delivery_at);
        }


        if ($status > -1) {
            $query->where('status',$status)->where('is_frozen',1);
        }

        $rows   =  $query->with('user')->paginate(15);
        $data = [
            'rows'=>$rows,
        ];
        return $this->response($data);
    }

    public function postCareSend(Request $request){
        $id = intval($request->input('id'));
        $box = BoxCare::find($id);
        if (!$box) {
            return $this->error('关怀盒子不存在');
        }
        if ($box->is_frozen!=1) {
            return $this->error('关怀盒子暂未激活');
        }
        $row = BoxDeliver::where('box_id',$id)->first();
        if ($row) {
            return $this->error('盒子已存在发货信息');
        }
        $name = safe2($request->input('name'));
        $no = safe2($request->input('no'));
        $estimate_at = safe2($request->input('estimate_at'));
        $row = new BoxDeliver;
        $row->box_id = $box->id;
        $row->name = $name;
        $row->no = $no;
        $row->estimate_at = $estimate_at;
        $row->delivery_at=date('Y-m-d H:i:s');
        $row->type='care';
        $row->Save();
        $box->status = 4;
        $box->Save();
        return $this->response('ok');
    }

    public function postCareChange(Request $request){
        $id = intval($request->input('id'));
        $param = $request->input('param');
        if (!is_array($param)) {
            return $this->error('参数错误');
        }
        $boxCare = BoxCare::find($id);
        if (!$boxCare) {
            return $this->error('关怀盒子不存在');
        }
        foreach ($param as $key => $value) {
            $boxCare->$key = $value;
            $boxCare->Save();
        }
        $boxCare->save();
        return $this->response('ok');
    }

    public function getBoxGive(Request $request)
    {
        $data['colours'] = Colour::get();
        return view('admin/box/box_give',$data);
    }

    public function postCareAddress(Request $request){
        $id = intval($request->input('id'));
        $boxCare = BoxCare::find($id);
        if (!$boxCare) {
            return $this->error('关怀盒子不存在');
        }
        $row = BoxAddress::where('type','care')->where('box_id',$id)->first();
        if (empty($row)) {
            return $this->error('该关怀盒子还未填写邮寄信息！');
        }
        return $this->response(compact('row'));
    }

}
